package com.shooter.server;

import com.alibaba.fastjson.JSONException;
import com.alibaba.fastjson.JSONObject;
import com.shooter.utils.PrintUtil;
import com.shooter.utils.StringUtil;
import com.shooter.utils.upload.SmartUpload;
import com.shooter.utils.upload.UploadFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by BLACK on 2018/3/27.
 */
public class ServletDispose {
    //客户请求
    private HttpServletRequest mRequest;
    //响应客户
    private HttpServletResponse mResponse;
    //application/json协议 中的数据
    private JSONObject applicationJson;
    //上传的文件
    private ArrayList<UploadFile> uploadFile = new ArrayList<>();
    //流中数据,上传文件的时候 参数会在流中获取
    private Map<String, String> streamParameters = new HashMap<>();
    //上传文件的错误信息
    private String uploadFileErrorInfo = null;//错误信息

    public ServletDispose(HttpServletRequest request, HttpServletResponse response) {
        this.mRequest = request;
        this.mResponse = response;
    }

    public HttpServletRequest getRequest() {
        return mRequest;
    }

    public void setRequest(HttpServletRequest mRequest) {
        this.mRequest = mRequest;
    }

    public HttpServletResponse getResponse() {
        return mResponse;
    }

    public void setResponse(HttpServletResponse mResponse) {
        this.mResponse = mResponse;
    }

    public String getUploadErrorInfo() {
        return uploadFileErrorInfo;
    }

    public void setUploadErrorInfo(String errorInfo) {
        this.uploadFileErrorInfo = errorInfo;
    }

    //返回字符
    protected void writerClient(String s) {
        try {
            getResponse().getWriter().print(s);
        } catch (IOException e) {
            PrintUtil.printError(e);
        }
    }

    //返回json数据
    protected void writerClient(JSONObject json) {
        try {
            getResponse().getWriter().print(json);
        } catch (IOException e) {
            PrintUtil.printError(e);
        }
    }

    //返回信息
    protected void writerClient(String code, String msg) {
        JSONObject json = new JSONObject();
        json.put(ServiceApi.CODE, code);
        json.put(ServiceApi.MSG, msg);
        writerClient(json);
    }

    //获取参数
    public String getValue(String key) {
        String value = "";
        if (applicationJson != null) {
            value = applicationJson.getString(key);
        }
        if (!StringUtil.checkStringNoNull(value)) {
            value = streamParameters.get(key);
        }
        if (!StringUtil.checkStringNoNull(value)) {
            value = getRequest().getParameter(key);
        }
        return value == null ? "" : value;
    }

    public void setSmartUpload(SmartUpload smartUpload) {
        Enumeration e = smartUpload.getRequest().getParameterNames();
        while (e.hasMoreElements()) {
            String key = (String) e.nextElement();
            String[] values = smartUpload.getRequest().getParameterValues(key);
            if (values != null && values.length != 0) {
                String value = values[0];
                streamParameters.put(key.substring(0, key.length() - 1), value);
            }
        }
    }
    //初始化application/json协议
    public void initApplicationJson() {
        getResponse().setHeader("Content-type", "application/json;charset=utf-8");
        StringBuffer buffer = new StringBuffer();
        try {
            BufferedReader reader = getRequest().getReader();
            String line;
            while ((line = reader.readLine()) != null) {
                buffer.append(line);
            }
            applicationJson = JSONObject.parseObject(buffer.toString());
        } catch (IOException e) {
            PrintUtil.printError(e);
        } catch (JSONException e) {
            PrintUtil.printError(e);
        }
    }

    public ArrayList<UploadFile> getUploadFile() {
        return uploadFile;
    }

    public void setUploadFile(ArrayList<UploadFile> uploadFile) {
        this.uploadFile = uploadFile;
    }
}
